package com.example.yin.controller;

import com.example.yin.common.ErrorMessage;
import com.example.yin.common.SuccessMessage;
import com.example.yin.config.OSSConfiguration;
import com.example.yin.service.OssService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;

@RestController
public class OssController {

    @Autowired
    private OssService ossService;

    @Autowired
    private OSSConfiguration ossConfiguration;



    @RequestMapping("/oss/upload")
    public Object upload(@RequestParam("file") MultipartFile file, @RequestParam("filepath")String filepath){

        String msg = ossService.uploadFile(file,filepath+file.getOriginalFilename());
        return new SuccessMessage<ObjectUtils.Null>("上传成功").getMessage();
    }

    @RequestMapping("/oss/vertify")
    public Object vertify(@RequestParam("filename")String filename){
        if(ossService.doesObjectExist(filename)){
            return new SuccessMessage<ObjectUtils.Null>("文件存在").getMessage();
        }
        return new ErrorMessage("文件不存在").getMessage();

    }

    @RequestMapping("/oss/download")
    public FileOutputStream download(@RequestParam("filename")String filename){
        FileOutputStream fout = null;
        ossService.exportFile(fout,filename);
        return fout;
    }

}
